<template>
  <div class="classify">
    <van-nav-bar title="商品分类" left-arrow @click-left="onClickLeft" />
    <section class="search-wrap" ref="searchWrap">
      <list-scroll class="nav-side-wrapper">
        <ul class="nav-side">
          <li
            v-for="(item, index) in data.categoryData"
            :key="index"
            :class="{ active: data.currentIndex === index }"
            @click="selectMenu(index)"
          >
            <span>{{ item.name.slice(0, 2) }}</span>
            <span>{{ item.name.slice(2) }}</span>
          </li>
        </ul>
      </list-scroll>
      <list-scroll class="search-content">
        <div class="swiper-container">
          <div class="swiper-wrapper">
            <template v-for="(it, index) in data.categoryData">
              <div
                class="swiper-slide"
                :key="index"
                v-if="data.currentIndex === index"
              >
                <img
                   @click="selectProduct(it.value)"
                  class="category-main-img"
                  :src="it.imgUrl"
                  v-if="it.imgUrl"
                />
                <div v-for="(i, index) in it.list" :key="index">
                  <p class="goods-title">{{ i.title }}</p>
                  <div class="category-list">
                    <div
                      class="product-item"
                      v-for="(val, index) in i.productList"
                      :key="index"
                      @click="selectProduct(val.value)"
                    >
                      <img class="item-img" :src="val.imgUrl" alt="" />
                      <p class="product-title">{{ val.title }}</p>
                    </div>
                  </div>
                </div>
              </div>
            </template>
          </div>
        </div>
      </list-scroll>
    </section>
  </div>
</template>
<script>
import listScroll from "@/components/ListScroll";
import { getCurrentInstance, reactive, onMounted } from "vue";
import { useRouter } from "vue-router";
export default {
  components: {
    listScroll,
  },

  setup() {
    const data = reactive({
      categoryData: [],
      currentIndex: 0,
    });
    const { ctx } = getCurrentInstance();
    const router = useRouter();
    const onClickLeft = () => console.log("返回");

    async function getCategoryData() {
      let res = await ctx.$http.get(
        "http://test.happymmall.com/category/categoryData"
      );
      console.log(res);
      if (res.status == 200) {
        const result = res.data;
        data.categoryData = result.categoryData;
      }
    }
    const selectMenu = (index) => {
      data.currentIndex = index;
    };
    const selectProduct = (sku) => {
      // console.log(3333)
      router.push({path:"/recommend",query:{sku}})
    };
    onMounted(() => {
      getCategoryData();
    });
    return { onClickLeft, data, selectMenu,selectProduct };
  },
};
</script>
<style lang="scss" scoped>
@import "../styles/mixin.scss";

.classify {
  height: 100%;
  .home-header {
    font-size: 18px;
    color: #3a3a3a;
    font-weight: 600;
    text-align: center;
    line-height: 50px;
    background-color: #fff;
  }
  .search-wrap {
    width: 100%;
    background: #fff;
    @include fj;
    .nav-side-wrapper {
      width: 88px;
      height: 100%;
      overflow: hidden;
      overflow-y: scroll;
      .nav-side {
        width: 100%;
        @include boxSizing;
        background: #f8f8f8;
        li {
          width: 100%;
          height: 77px;
          text-align: center;
          font-weight: 600;
          font-size: 14px;
          display: flex;
          justify-content: center;
          align-items: center;
          color: #949497;
          flex-direction: column;
          &.active {
            color: #d8182d;
            border-left: 3px solid transparent;
            border-color: #d8182d;
            background: #fff;
          }
        }
      }
    }
    .search-content {
      width: 80%;
      height: 100%;
      padding: 0 16px;
      background: #fff;
      padding-bottom: 30px;
      @include boxSizing;
      .swiper-container {
        width: 100%;
        .swiper-slide {
          width: 100%;
          padding-top: 20px;
          .goods-title {
            font-size: 14px;
            color: #d8182d;
            font-weight: 600;
            padding-bottom: 10px;
          }
          .category-main-img {
            width: 100%;
          }
          .category-list {
            display: flex;
            flex-wrap: wrap;
            flex-shrink: 0;
            width: 100%;
            .catogory-title {
              width: 100%;
              font-size: 34px;
              font-weight: 500;
              padding: 40px 0;
            }
            .product-item {
              width: 33%;
              margin-bottom: 20px;
              text-align: center;
              font-size: 30px;
              .item-img {
                width: 65px;
                height: 80px;
              }
              .product-title {
                color: #3a3a3a;
                font-size: 11px;
                font-weight: 600;
                // width: 50%;
                white-space: nowrap;
              }
            }
          }
        }
      }
    }
  }
}
</style>
